Spring boot集成Elasticsearch填坑之日期类型

Spring boot集成Elasticsearch填坑之日期类型

环境

Java: 1.8
Spring Boot: 2.3.2.RELEASE
Elasticsearch: 7.9.2

问题

当实体类设置日期类型为Date时,保存数据没有问题,但查询时报错:

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2007-08-09 of type java.time.format.Parsed

	at java.time.Instant.from(Instant.java:378)
	at org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter.parse(ElasticsearchDateConverter.java:125)
	at org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentProperty$1.read(SimpleElasticsearchPersistentProperty.java:160)
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readValue(MappingElasticsearchConverter.java:281)
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter$ElasticsearchPropertyValueProvider.getPropertyValue(MappingElasticsearchConverter.java:869)
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readProperties(MappingElasticsearchConverter.java:260)
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readEntity(MappingElasticsearchConverter.java:192)
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.read(MappingElasticsearchConverter.java:172)
	at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.read(MappingElasticsearchConverter.java:81)
	at org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate$ReadDocumentCallback.doWith(AbstractElasticsearchTemplate.java:602)
	at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.get(ElasticsearchRestTemplate.java:166)

解决方案

使用java.time包下的日期类型替代Date